package com.shudai.largescreen.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.fastjson.JSON;
import com.shudai.largescreen.common.R;
import com.shudai.largescreen.pojo.vo.ExcelListVo;
import com.shudai.largescreen.pojo.vo.ExcelSalesmanVo;
import com.shudai.largescreen.pojo.vo.NotSuExcelVo;
import com.shudai.largescreen.service.NotSuExcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api")
@Api(tags = "要求的表格",value = "要求的表格")
@RequiredArgsConstructor
public class NotSuExcel {


    private  final NotSuExcelService notSuExcelService;

    @GetMapping("/NotSuExcel/salesman-excel")
    @ApiOperation(value = "下载excel")
    public R getSalesmanExcel(HttpServletResponse response,
                              HttpServletRequest request) throws IOException {


        //1，找出模板文件，并转化为输入流：templateFileInputStream； http://localhost:8800/api/NotSuExcel/salesman-excel
        List<NotSuExcelVo> data = notSuExcelService.getAll();
        System.out.println(JSON.toJSON(data));
        org.springframework.core.io.Resource resource = new ClassPathResource("复购资料.xlsx");
        InputStream templateFileInputStream = resource.getInputStream();
        //2，outputStream：要导出的文件的输出流；
        OutputStream outputStream = response.getOutputStream();
        ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(templateFileInputStream).build();
        try {
            WriteSheet writeSheet = EasyExcel.writerSheet("复购资料").build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            //3，往sheet表中填充数据：
            excelWriter.fill(data, fillConfig, writeSheet);
            //4，设置输出流格式以及文件名：
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json;charset=utf-8");
            String fileName = URLEncoder.encode("复购资料导出", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            log.info("业务订单导出end");
        }catch (Exception e) {
            log.error("业务订单导出异常打印:", e);
        } finally {
            //5，最后，千万别忘记finish，关闭IO流；
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }

        return R.ok();
    }
}
